import math

import numpy as np
import pandas as pd
from datetime import datetime

pd.set_option('display.max_rows', None)
# 设置pandas显示的列数
pd.set_option('display.max_columns', 20)
# 让dataframe打印不换行
pd.set_option('display.width', 5000)


def getTime(opens, stops):
    i = 0
    size = len(opens)
    dict_kv = {}
    while i < size:
        # print(opens[i],stops[i],end='\t')
        t1 = datetime.strptime(opens[i], '%Y-%m-%d %H:%M:%S')
        t2 = datetime.strptime(stops[i], '%Y-%m-%d %H:%M:%S')
        time_m = math.ceil((t2 - t1).seconds / 60)
        dict_kv[time_m] = dict_kv.get(time_m, 0) + 1
        i += 1
    return dict_kv


# data = pd.read_csv('E:/Temp/yellow_tripdata_2020-06.csv',low_memory=False,nrows=111)
# data = pd.read_csv('F:/ChromeDown/yellow_tripdata_2014-01.csv',nrows=111)
data = pd.read_csv('F:/ChromeDown/yellow_tripdata_2014-01.csv',nrows=100000)
print(data.head())
orderSize = data.shape[0]
print('orderSize:', orderSize)

pickup_list = data[' pickup_datetime'].tolist()
dropoff_list = data[' dropoff_datetime'].tolist()
# pickup_list = data['tpep_pickup_datetime'].tolist()
# dropoff_list = data['tpep_dropoff_datetime'].tolist()

time = getTime(pickup_list, dropoff_list)
print(str(time))

keys = []
a = 0  # <=5分钟
b = 0  # <5 <=10分钟
c = 0  # <10 <=15分钟
d = 0  # <15 <=20分钟
e = 0  # <20 <=25分钟
f = 0  # <25 <=30分钟
g = 0  # <30 <=60分钟
h = 0  # <60分钟

for item in time.items():
    keys.append(item[0])
    if item[0] <= 5:
        a += item[1]
    elif 5 < item[0] and item[0] <= 10:
        b += item[1]
    elif 10 < item[0] and item[0] <= 15:
        c += item[1]
    elif 15 < item[0] and item[0] <= 20:
        d += item[1]
    elif 20 < item[0] and item[0] <= 25:
        e += item[1]
    elif 25 < item[0] and item[0] <= 30:
        f += item[1]
    elif 30 < item[0] and item[0] <= 60:
        g += item[1]
    elif 60 < item[0]:
        h += item[1]
print(keys)
keys.sort()
print(keys)

print('订单时间(0,5]分钟的订单数:', a, '占总订单比例:', '占总订单比例:', a / orderSize * 100, '%')
print('订单时间(5,10]分钟的订单数:', b, '占总订单比例:', '占总订单比例:', b / orderSize * 100, '%')
print('订单时间(10,15]分钟的订单数:', c, '占总订单比例:', '占总订单比例:', c / orderSize * 100, '%')
print('订单时间(15,20]分钟的订单数:', d, '占总订单比例:', '占总订单比例:', d / orderSize * 100, '%')
print('订单时间(25,25]分钟的订单数:', e, '占总订单比例:', '占总订单比例:', e / orderSize * 100, '%')
print('订单时间(25,30]分钟的订单数:', f, '占总订单比例:', f / orderSize * 100, '%')
print('订单时间(30,60]分钟的订单数:', g, '占总订单比例:', g / orderSize * 100, '%')
print('订单时间(60,∞]分钟的订单数:', h, '占总订单比例:', h / orderSize * 100, '%')
# print(size)
# for t2 in dropoff_list:
t1 = datetime.strptime('2020-06-01 00:49:34', '%Y-%m-%d %H:%M:%S')
t2 = datetime.strptime('2020-06-01 00:55:35', '%Y-%m-%d %H:%M:%S')
print((t2 - t1).seconds, (t2 - t1).seconds / 60, math.ceil((t2 - t1).seconds / 60))

# print(data.head())
